// pages/room/room.js
const config = require('../../config.js')
const http = require('../../http.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topLikeData: [{
      title: "等你挑战，参与冲榜",
      tag: false
    }, {
      title: "等你挑战，参与冲榜",
      tag: false
    }, {
      title: "等你挑战，参与冲榜",
      tag: false
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("onLoad options=", options)
    if (options.roomId) {
      this.setData({
        roomId: options.roomId
      })
    }
    if (!app.globalData.isLogin) {
      wx.navigateTo({
        url: '../login/login',
      })
      return
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMeetingMsg(this.data.roomId)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  onClick: function (e) {
    console.log(e)
    let type = parseInt(e.currentTarget.dataset.type)
    let itemId = parseInt(e.currentTarget.dataset.id)
    switch (type) {
      case 1:
        wx.navigateTo({
          url: '../hear/hear?roomId='+this.data.roomId+"&itemId="+itemId
        })
        break
      case 2:
        wx.setStorageSync('meetingData', this.data.meetingData)
        wx.navigateTo({
          url: "../lawlist/lawlist?roomId="+this.data.roomId
        })
        break
    }

  },

  getMeetingMsg: function (rId) {
    var that = this
    http.request(
      config.meetings + "/" + rId + "/brief",
      'POST', null,
      function (res) {
        if (res.data != null) {
          let unfinished = res.data.totalItem - res.data.totalFinished
          res.data.unfinished = unfinished
          let topLikeList = res.data.topLikeList
          if (topLikeList != null && topLikeList.length > 0) {
            for(var i = 0;i<topLikeList.length;i++){
              topLikeList[i].tag = true
              that.data.topLikeData[i] = topLikeList[i]
            }
          }
          that.setData({
            meetingData: res.data,
            topLikeData:that.data.topLikeData
          })
        }

      },
      function (err) {
        wx.showToast({
          title: err,
          icon: 'none',
        })
      }
    )
  },

  onShareAppMessage: function (res) {
    this.shareCount()
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log("onShareAppMessage", res.target)
    }
    return {
      title: '邀好友助力，参与读法PK，等你挑战！',
      path: '/pages/room/room?roomId='+this.data.roomId
    }
  },

  shareCount:function(){
    console.log("分享次数")
    var that = this
    http.request(
      config.meetings + "/" + this.data.roomId + "/share",
      'POST', null,
      function (res) {
        if (res.data != null) {
          that.data.meetingData.countShare = res.data
          that.setData({
            meetingData: that.data.meetingData
          })
        }
      },
      function (err) {
        wx.showToast({
          title: err,
          icon: 'none',
        })
      }
    )
  }
})